/*
Web: http://www.jsForms.com
Copyright (C) 2008  Igor Demyanov (mailto:Igor.Demyanov@gmail.com)
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
jsForms.Components.TreeView = {
	Init: function(){
		//console.log(this._Name + ':TreeView.Init');
		if (!this.Properties.SelectedClass)
		{
			this.Properties.SelectedClass="treeview-node-select";
		}
		this.bind('click', this, this.eventClick);
		return this;
	},
	eventClick: function(e){
		if (!e.data) return;
		var el = e.target;
		var isHitArea = (el.getAttribute('hitarea')!= null);
			do
			{
				var obj = jsForms.GetObject(el);
				if (obj) {
					switch (obj._Type) {
						case 'TreeViewNode':
							var node = e.data.GetTreeNodeByElement.call(e.data,el);
							if (isHitArea) {
								if (node) {
									node.Toggle();
								}
							}
							else {
								e.data.Select.call(e.data,node);
							}
						case 'TreeView':
							return;	}
				}
				el = el.parentNode;
			}while(el);
	},
	Select:function(item)
	{
		if (!item || item._Type != "TreeViewNode") {
			return this;
		}
		//var selItem = this.GetTreeNodeById(id);
		if (this.P.SelectedItem != item)
		{
			if (this.P.SelectedItem)
			{
				this.P.SelectedItem.C.text.removeClass(this.Properties.SelectedClass);
			}
			this.P.SelectedItem = item;
			if (this.P.SelectedItem)
			{
				this.P.SelectedItem.C.text.addClass(this.Properties.SelectedClass);
			}
			this._ExecEvent.call(this,'onChangeSelect',item);
		}
		return this;
	},
	GetParent:function(node)
	{
		if (!node || node._Type != "TreeViewNode") {
			return;
		}
		var parent = node.parent();
		if (parent.length==0)
		{
			return;
		}
		return this.GetTreeNodeByElement(parent[0]);
	},
	RemoveNode:function(node)
	{
		if (!node || node._Type != "TreeViewNode") {
			return this;
		}

		var parentNode = this.GetParent(node);
		
		if (parentNode)
		{
			parentNode.RemoveChild(node);
		}
		return this;
	},
	GetTreeNodeByElement:function(element)
	{
		var el = element;
		if (!el) return;
		//var path = [];
			do
			{
				var obj = jsForms.GetObject(el);
				if (obj) {
					return obj;
					/*switch (obj._Type) {
						case 'TreeViewNode':
							var id =obj.GetPathId();
							if (id == undefined) {
								return;
							}
							path.push(id);
						case 'TreeView':
							/*if (path.length == 0) {
								return;
							}
							var node = this;
							for (var i = path.length - 1; i >= 0; i--) {
								node = node.GetChild(path[i]);
								if (!node) {
									return;
								}
							}
							return node;
					}*/
				}
				el= el.parentNode;
			}while(el);
	},
	Clear: function()
	{
		if (this.P.Items) {
			var content = this.C.content[0];
			for (var i in this.P.Items)
			{
				var item = this.P.Items[i];
				if (item) {
					content.removeChild(item[0]);
				}
			}
			delete (this.P.Items);
		}
		this.P.Items={};
		this.P.ItemsCount=0;
	},
	
	AddChild:function(node,neighbourNode,option)
	{
		if (!node || node._Type != "TreeViewNode") {
			return this;
		}
		if (!this.Properties.Items)
		{
			this.Clear();
		}
		if (neighbourNode) {
			if (option == "before") {
				neighbourNode.before(node);
			}else
			{
				neighbourNode.after(node);
			}
		}
		else {
			this.C.content[0].appendChild(node[0]);
		}
		this.P.Items[this.P.ItemsCount] = node;
		node.SetPathId(this.P.ItemsCount);
		this.P.ItemsCount++;
		return this;
	},
	RemoveChild:function(node)
	{
		if (!node || node._Type != "TreeViewNode") {
			return this;
		}
		if (this.P.Items == undefined)
		{
			return this;
		}
		
		var pid = node.GetPathId();
		var item = this.P.Items[pid];

		if (item!=undefined && item[0] == node[0])
		{
			//node.Clear();
			this.C.content[0].removeChild(item[0]);
			this.P.ItemsCount--;
			delete(this.P.Items[pid]);
		}
		return this;
	},
	GetChild:function(pid)
	{
		if (!this.Properties.Items)
		{
			this.Clear();
		}
		return this.Properties.Items[pid];
	},
	CreateNode: function(id,text)
	{
		var newNode = jsForms.CreateComponent('TreeViewNode');
		if (text != undefined) {
			newNode.SetText(text);
		}
		if (id != undefined) {
			newNode.SetId(id);
		}
		return newNode;
	}
	/*
	PrevChild:function(node)
	{
		if (!node || node._Type != "TreeViewNode") {
			return;
		}
		var i = this.Properties.Items[node.GetPathId()];
		if (!i)
		{
			return;
		}
		if (i[0] == node[0])
		{
			var ii = node.prev();
			if (ii.length ==0)
			{
				return;
			}
			var pid = ii[0].getAttribute('pid');
			if (pid)
			{
				return this.Properties.Items[pid];
			}
		}
	},
	NextChild:function(node)
	{
		if (!node || node._Type != "TreeViewNode") {
			return;
		}
		var i = this.Properties.Items[node.GetPathId()];
		if (!i)
		{
			return;
		}
		if (i[0] == node[0])
		{
			var ii = node.next();
			if (ii.length ==0)
			{
				return;
			}
			var pid = ii[0].getAttribute('pid');
			if (pid)
			{
				return this.Properties.Items[pid];
			}
		}
	}*/
};

jsForms.Components.TreeViewNode={
	Init: function(){},
	SetText:function(value)
	{
		this.C.text[0].innerHTML = value;
		return this;
	},
	GetText:function()
	{
		return this.C.text[0].innerHTML;
	},
	SetPathId:function(id)
	{
		this.P.pid = id;
		//this[0].setAttribute('pid', id);
		return this;
	},
	GetPathId:function()
	{
		//return parseInt(this[0].getAttribute('pid'));
		return this.P.pid;
	},
	SetId:function(value)
	{
		if (this.P.id != value) {
			this.P.id = value;
			//this[0].setAttribute('iid', value);
		}
		return this;
	},
	GetId:function()
	{
		return this.P.id;
	},
	Toggle:function()
	{
		//console.log(this._Name+':TreeViewNode.Toggle');
			if (!this.P.isCollapse)
			{
				this.Collapse();
			}else
			{
				this.Expand();
			}
		return this;
	},
	Expand: function(){
		//console.log(this._Name+':TreeViewNode.Expand');
		this.P.isCollapse = false;
		this.C.hitarea.removeClass('TreeViewHitClose');
		this.C.content.show();
		return this;
	},
	Collapse:function()
	{
		//console.log(this._Name+':TreeViewNode.Collapse');
		this.P.isCollapse = true;
		this.C.hitarea.addClass('TreeViewHitClose');
		this.C.content.hide();
		return this;
	}
};
jsForms.Components.TreeViewNode.AddChild = jsForms.Components.TreeView.AddChild;
jsForms.Components.TreeViewNode.RemoveChild = jsForms.Components.TreeView.RemoveChild;
jsForms.Components.TreeViewNode.Clear = jsForms.Components.TreeView.Clear;
jsForms.Components.TreeViewNode.GetChild = jsForms.Components.TreeView.GetChild;

// -----------------------------------------------
// Compilation INFO
// -----------------------------------------------
jsForms.CreateCompileInfoByObject(
{
	Name: 'TreeView',
	Properties: {
		SelectedClass: {
			Type: 'string',
			Access: 'DIRECT'
		}
	},
	Events: {
		onChangeSelect:{}
	}
});

jsForms.CreateCompileInfoByObject(
{
	Name: 'TreeViewNode',
	Properties: {
		Text: {
			Type: 'string',
			Access: 'getset'
		},
		Id: {
			Access: 'getset'
		}
	},
	Events: {}
});
